<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <title>epg测试页面</title>

    <link media="all" type="text/css" href="./css/ui.css?v=2014122229711" rel="stylesheet">
    <link media="all" type="text/css" href="./css/pps.css?v=2014122229711" rel="stylesheet">
    <script src="./js/core.js?v=2014122229711" type="text/javascript"></script>
    <script src="./js/ui.js?v=2014122229711" type="text/javascript"></script>
    <script src="./js/qui.js?v=2014122229711" type="text/javascript"></script>
<body>
<!--<svg id="drawSvg">-->
<!--<rect x="15" y="15" width="40" height="40" fill="red">-->
<!--<set attributeName="fill" to="blue" begin="click"/>-->
<!--</rect>-->
<!--</svg>-->
<div id="panel"></div>
</body>
<script type="text/javascript">
    window.LogInfo = {
        decodeInfoArr: []
    }

    window.MathUtil = {
        showU32: function (str) {
            var value = this.appendZero( str.charCodeAt(0).toString(16)) + this.appendZero( str.charCodeAt(1).toString(16))+ this.appendZero( str.charCodeAt(2).toString(16))+ this.appendZero( str.charCodeAt(3).toString(16));
            return Number("0x"+value);
        },
        showU24: function (str) {
            var value = this.appendZero( str.charCodeAt(0).toString(16)) + this.appendZero( str.charCodeAt(1).toString(16))+ this.appendZero( str.charCodeAt(2).toString(16));
            return Number("0x"+value);
        },
        showU16: function (str) {
            var value = this.appendZero( str.charCodeAt(0).toString(16)) + this.appendZero( str.charCodeAt(1).toString(16));
            return Number("0x"+value);
        },
        showU8: function (str) {
            var value = this.appendZero( str.charCodeAt(0).toString(16));
            return Number("0x"+value);
        },
        appendZero:function( str){
            str = "0000"+str;
            return str.substring( str.length -4 ,str.length );
        }
    }
    window.FlvTagDecoder = {
        preTagSize: function (entity, startIndex) {
            LogInfo.decodeInfoArr.push("preTagSize:" + MathUtil.showU32(entity.responseText.substring(startIndex, startIndex + 4)));
        },
        decodeType: function (entity, index) {
            var tagValue = entity.responseText.charCodeAt(index);
            var descValue = "";
            switch (tagValue) {
                case 8:
                    descValue = "音频"
                    break;
                case 9:
                    descValue = "视频"
                    break;
                case 18:
                    descValue = "script data "
                    break;
                default :
                    descValue = "unknown " + tagValue
                    break;
            }
            LogInfo.decodeInfoArr.push("tagValue:" + descValue);
            return tagValue;
        },
        decodeData: function (entity, startIndex) {
            LogInfo.decodeInfoArr.push("tag Data size:" + MathUtil.showU24(entity.responseText.substring(startIndex, startIndex + 3)));
        },
        decodeTimestamp: function (entity, startIndex) {
            LogInfo.decodeInfoArr.push("Timestamp:" + MathUtil.showU24(entity.responseText.substring(startIndex, startIndex + 3)));
        },
        decodeTimestamp_ex: function (entity, startIndex) {
            LogInfo.decodeInfoArr.push("Timestamp_ex:" + MathUtil.showU8(entity.responseText.substring(startIndex,startIndex+1)));
        },
        decodeStreamId: function (entity, startIndex) {
            LogInfo.decodeInfoArr.push("streamId:" + MathUtil.showU24(entity.responseText.substring(startIndex, startIndex + 3)));
        },
        decodeScriptData: function (entity, startIndex) {
            LogInfo.decodeInfoArr.push("第一个AMF包类型:" + entity.responseText.charCodeAt(startIndex + 0));
            startIndex += 1;
            LogInfo.decodeInfoArr.push("onMetaData长度:" + MathUtil.showU16(entity.responseText.substring(startIndex, startIndex + 2)));
            startIndex += 2;
            var onMetaData = entity.responseText.substring(startIndex, startIndex + 10);
            LogInfo.decodeInfoArr.push("onMetaData长度:" + onMetaData);
            startIndex += 10;
            LogInfo.decodeInfoArr.push("第二个AMF包类型:" + entity.responseText.charCodeAt(startIndex));
            startIndex += 1;
            LogInfo.decodeInfoArr.push("UI32类型值:" + MathUtil.showU32(entity.responseText.substring(startIndex, startIndex + 4)));
            startIndex += 4;
            console.log( "startIndex:"+startIndex)
        }
    }
    window.FlvDecoder = {
        entity: null,
        decodeHeader: function () {
            var flvInfo = this.entity.responseText.substring(0, 3);
            LogInfo.decodeInfoArr.push("flvInfo:" + flvInfo);
            var version = this.entity.responseText.charCodeAt(3)
            LogInfo.decodeInfoArr.push("version:" + version);
            var value = this.entity.responseText.charCodeAt(4);
            LogInfo.decodeInfoArr.push("有音频:" + ((value & 4) == 4));
            LogInfo.decodeInfoArr.push("有视频:" + ((value & 1) == 1));
            var headerSize = MathUtil.showU32( this.entity.responseText.substring(5, 9));
            LogInfo.decodeInfoArr.push("headerSize:" + headerSize);
        },
        decodeBody: function () {
            var startIndex = 9;
            this.decodeTag(this.entity, startIndex);
        },
        decodeTag: function (entity, startIndex) {
            var before = startIndex;
            //tag header
            FlvTagDecoder.preTagSize(entity, startIndex)
            startIndex += 4;
            var tagValue = FlvTagDecoder.decodeType(entity, startIndex);
            startIndex += 1;
            FlvTagDecoder.decodeData(entity, startIndex);
            startIndex += 3;
            FlvTagDecoder.decodeTimestamp(entity, startIndex);
            startIndex += 3
            FlvTagDecoder.decodeTimestamp_ex(entity, startIndex);
            startIndex += 1
            FlvTagDecoder.decodeStreamId(entity, startIndex);
            startIndex += 3

            //tag body
            switch (tagValue) {
                case 8:
                    break;
                case 9:
                    break;
                case 18:
                    FlvTagDecoder.decodeScriptData(entity, startIndex);
                    startIndex += 13;
                    break;
                default :
                    break;
            }

        },
        bindData: function (data) {
            this.entity = data;
            this.isLoaded();
        },
        startDecode: function () {
            LogInfo.decodeInfoArr = [];
            this.decodeHeader();
            this.decodeBody();
            console.log(LogInfo.decodeInfoArr);
        },
        isLoaded: function () {
            var startTime = Date.now();
            var self = this;
            var timeId = setInterval(function () {
                if (self.entity.readyState == 4) {
                    clearInterval(timeId);
                    console.log("cost time" + ( Date.now() - startTime))
                }
            }, 20)
            console.log(this.entity.readyState)
        }
    }
    var temp = null;

    function loadFlv() {
        temp = $.get("./swfs/1506415846-.flv");
        window.FlvDecoder.bindData(temp);
    }
    $(document).ready(function () {
        loadFlv();
    })
</script>
</html>